using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DailyTransaction
{
    public partial class frmInputUser : Form
    {
        private BAO bao = new BAO();
        public frmInputUser()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtUserID.Text == null || txtUserID.Text.Trim() == "")
            {
                MessageBox.Show("User ID must not be empty \n Please input userID!");
                txtUserID.Focus();
                return;
            }

            if (txtPassword.Text == null || txtPassword.Text.Trim() == "")
            {
                MessageBox.Show("Password must not be empty \n Please input password!");
                txtUserID.Focus();
                return;
            }

            if (cbRole.Text == "")
            {
                MessageBox.Show("Role must not be empty \n Please input role!");
                txtUserID.Focus();
                return;
            }

            bool isExisted = bao.Check_UserID(txtUserID.Text);
            if (isExisted == true)
            {
                MessageBox.Show("User ID is existed!");
                txtUserID.Focus();
                return;
            }
            string fname = txtFirstname.Text != null ? txtFirstname.Text.Trim(): "";
            string lname = txtLastname.Text != null ? txtLastname.Text.Trim(): "";
            string encodedPass = bao.EncodePassword(txtPassword.Text);
            int role = int.Parse(cbRole.Text);
            try
            {
                bao.Insert_User_account(fname, lname, txtUserID.Text.Trim().ToLower(), encodedPass, role);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            MessageBox.Show("Added a new user successfully!");

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
    }
}